<?php

namespace app\common\model;

use app\admin\controller\Auth;
use think\Exception;
use think\Model;

class Rule extends Base
{


    protected $insert = [

    ];

    //定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';


    /**
     * 获取菜单
     * 去除多余节点 保留树型节点
     */
    public function getMenus()
    {

        //获取一级菜单  有缓存数据则用缓存中获取
        //缓存时间一小时
        //     $topMenu=empty(Cache::get('top_menu'))?$this->field('id,name,pid,icon,url,sort,create_time')->where('pid',0)->cache('top_menu')->order('sort asc , create_time desc')->select():Cache::get('top_menu');

        $auth = new Auth();
        $condition = [];
        $condition['hidden'] = 0;
        $condition['id'] = ['in', $auth->getAuthOfUser()];
        $topMenu = $this->where($condition)->order('sort asc')->select(); //获取不隐藏菜单
        $Menus = getTree($topMenu, 0);
        foreach ($Menus as $key => $value) {
            if ($value['pid'] == '') {
                unset($Menus[$key]);
            }
        }
        return $Menus;
    }

    public function getRules()
    {
        $topMenu = $this->select(); //获取不隐藏菜单
        $Menus = getTree($topMenu, 0);

        foreach ($Menus as $key => $value) {
            if ($value['pid'] == '') {
                unset($Menus[$key]);
            }
        }
        return $Menus;
    }


    public static function menuId($url)
    {

        //todo 针对无限级菜单，进行递归查询
        //todo 目前只支持二级菜单查询
        $url = strtolower($url);
        //二级id获取
        $data = self::where('url', $url)->field('id,pid')->find();
        return $data;
    }

    public static function Breadcrumbs($url)
    {
        $url = strtolower($url);
        //根据url查找二级菜单相关信息
        $second_data = self::where('url', $url)->field('name,url,pid,id')->find();

        //根据查找到的二级目录信息查新一级目录信息

        if (!empty($second_data)) {
            $top_data = self::where('id', $second_data['pid'])->field('name,icon')->find();
        } else {
            //todo 配置默认面包屑导航
            $top_data = ['name' => '首页', 'icon' => 'fa-home'];
        }
        return ['top' => $top_data, 'second' => $second_data];
    }

    /**
     *
     * 获取一级菜单
     * return [ id=>name]
     */
    public function getTopMenus()
    {
        $data = $this->where('pid', 0)->select();
        $result = [];
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                $result[$value['id']] = $value['name'];
            }
        }
        return $result;
    }

    /**
     * 根据id获取某一菜单详情
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     */
    public function getMenuById($id)
    {

        return $this->where('id', $id)->find();
    }


    public function delMenus($id)
    {

        //检测id是否为一级菜单
        //一级菜单下面是否有其他菜单 此情况下不允许删除
        if (is_array($id)) {
            $this->startTrans();
            try {
                foreach ($id as $key => $value) {
                    //   var_dump($value);
                    if ($this->checkIsAllowDelete($value)) {
                        self::destroy($value);
                    }
                }
            } catch (Exception $e) {
                $this->rollback();
                return false;
            }
            $this->commit();
            return true;
        } else {
            if ($this->checkIsAllowDelete($id) && self::destroy($id)) {
                return true;
            }
            return false;
        }
    }

    /**
     * 检测是否一级菜单， 检测一级菜单下是否有子菜单
     * @param $id
     * @return bool
     */
    protected function checkIsAllowDelete($id)
    {
        //一级菜单下无子菜单 可以删除
        $count = $this->where('pid', $id)->count("id");
        if ($count == 0) {
            return true;
        }

        return false;
    }

}